package com.myteam.fugaocrm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.myteam.fugaocrm.modle.FgPartnerCompany;
import com.myteam.fugaocrm.modle.FgPartnerContact;
import com.myteam.fugaocrm.modle.FgPartnerProduct;
import com.myteam.fugaocrm.service.PartnerCompanyServiceImpl;
import com.myteam.fugaocrm.service.PartnerContactServiceImpl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 供应商联系人Controller
 * @author BZX
 */
@Controller
public class PartnerContactController {
	@Resource protected PartnerContactServiceImpl partnerContactServiceImpl;
	@Resource protected PartnerCompanyServiceImpl partnerCompanyServiceImpl;
	
	@RequestMapping("partner/contact/all")
	@ResponseBody
	public ModelAndView all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<FgPartnerContact> fgPartnerContactList = partnerContactServiceImpl.all();
		
		List<FgPartnerCompany> fgPartnerCompanyList = partnerCompanyServiceImpl.all();
		request.getSession().setAttribute("fgPartnerCompanyList",fgPartnerCompanyList);
		
		ModelAndView view = new ModelAndView();
		view.addObject("fgPartnerContactList", fgPartnerContactList);
		view.setViewName("arch_supplier_contacts.jsp");
		return view;
	}
	
	@RequestMapping("partner/contact/delete")
	@ResponseBody
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer contactId = Integer.parseInt(request.getParameter("contactId").toString());
		
		partnerContactServiceImpl.deleteOne(contactId);
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/partner/contact/all");
		return view;
	}
	
	@RequestMapping("partner/contact/insertorupdate")
	@ResponseBody
	public ModelAndView insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String contactIdStr = request.getParameter("contactId")+"";
		String partnerCompanyId = request.getParameter("partnerCompanyId")+"";
		String contactName = request.getParameter("contactName")+"";
		String contactMobile = request.getParameter("contactMobile")+"";
		String contactEmail = request.getParameter("contactEmail")+"";
		
		//校验
		
		
		//插入或更新
		Date date = new Date();
		String staffName = request.getSession().getAttribute("staffName")+"";
		Map<String, Object> params = new HashMap<String, Object>();
		
		if("".equals(contactIdStr)) {
			//insert
			params.put("partnerCompanyId", partnerCompanyId);
			params.put("contactName", contactName);
			params.put("contactMobile", contactMobile);
			params.put("contactEmail", contactEmail);
			params.put("createdDate", date);
			params.put("createdUser", staffName);
			partnerContactServiceImpl.insert(params);
		}else {
			//update
			Integer contactId = Integer.parseInt(contactIdStr);
			params.put("contactId", contactId);
			params.put("partnerCompanyId", partnerCompanyId);
			params.put("contactName", contactName);
			params.put("contactMobile", contactMobile);
			params.put("contactEmail", contactEmail);
			params.put("modifiedDate", date);
			params.put("modifiedUser", staffName);
			partnerContactServiceImpl.update(params);
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/partner/contact/all");
		return view;
	}
	
	/**
	 * 根据供应商公司ID查询该公司全部联系人
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("partner/contact/getlistbycompanyid")
	@ResponseBody
	public List<FgPartnerContact> getListByCompanyId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer partnerCompanyId = Integer.parseInt(request.getParameter("partnerCompanyId").toString());
		List<FgPartnerContact> list =  partnerContactServiceImpl.selectList(partnerCompanyId);
		return list;
	}
}
